package com.hydra.framework.event.kvo.list;

import android.util.Log;

import com.hydra.framework.event.kvo.KvoFieldAnnotation;
import com.hydra.framework.event.kvo.KvoSource;

import java.util.List;

/**
 * Created by Hydra.
 *
 * 对常用的分页数据的封装
 */
public class KvoPageList<T> extends KvoSource {

    private static final String KVO_PAGELIST_TAG = "KvoPageList";

    public static final String kvo_datas = "datas";
    @KvoFieldAnnotation(name = kvo_datas)
    public KvoList<T> datas = new KvoList<>(this, kvo_datas);

    public static final String kvo_hasMore = "hasMore";
    @KvoFieldAnnotation(name = kvo_hasMore)
    public boolean hasMore = false;

    public static final String kvo_isLoading = "isLoading";
    @KvoFieldAnnotation(name = kvo_isLoading)
    public boolean isLoading = false;

    //这里注意，默认要从第一页开始，不然就会出错
    public int pageNum = 0;

    public void reset() {
        datas.clear();
        setValue(kvo_hasMore, false);
        setValue(kvo_isLoading, false);
        pageNum = 0;
    }

    public void setLoading(boolean isLoading) {
        setValue(kvo_isLoading, isLoading);
    }

    public void combineList(List<T> list, int pageNum, boolean hasMore) {
        if(pageNum == 1) {
            //第一页，refresh
            this.pageNum = pageNum;

            setValue(kvo_hasMore, hasMore);

            datas.clear();
            datas.addAll(list);
        } else {
            if(pageNum == this.pageNum + 1) {
                this.pageNum = pageNum;

                setValue(kvo_hasMore, hasMore);

                datas.addAll(list);
            } else {
                Log.e(KVO_PAGELIST_TAG, "error page list combine pre page is : "
                        + this.pageNum + " new page is : " + pageNum);
            }
        }
    }
}
